package cn.ekuma.epos.datalogic.util;

import java.util.List;

import com.caucho.hessian.client.HessianConnectionException;
import com.caucho.hessian.client.HessianRuntimeException;
import com.openbravo.bean.Product;
import com.openbravo.bean.viewbean.ProductStoreView;
import com.openbravo.data.basic.BasicException;

import cn.ekuma.epos.datalogic.DataLogicPDA;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.datalogic.local.dao.ProductDAO;
import android.os.Handler;

public class ProductUtil {

	public static final int MSG_SUCCESS = 0;
	public static final int MSG_FAILURE = 1;
	
	
	public static final int BUILDERTYPE_BASE = 0X01;
	public static final int BUILDERTYPE_BUYPROCE = 0X02;
	public static final int BUILDERTYPE_STORE = 0X04;
	public static final int BUILDERTYPE_ERP = 0x06;
	
	
	public static void findProduct(String code,boolean islocal,Handler productHander,ProductDAO productDAO,int productBuilderType){
		if(islocal)
			findProductWithLocal(code,productHander,productDAO,productBuilderType);
		else
			findProductWithRomote(code,productHander,productBuilderType);
	}
	
    public static void findProductWithLocal(String codeOrReference,Handler productHander,ProductDAO productDAO,int productBuilderType){
    	List<Product> listp=null;
    	 if(codeOrReference.length()==14&&codeOrReference.startsWith("1"))
			   listp = productDAO.getBomProductForIncludeList(codeOrReference.substring(1));
		   else if(codeOrReference.length()==14&&codeOrReference.startsWith("2"))
			   listp = productDAO.getBomIncludeProductList(codeOrReference.substring(1));
		  else
			  listp = productDAO.getProductInfoByCode(codeOrReference);

		   Product oProduct;
	        //当按条码找不到商品时，如果输入的可能是编号（长度小于9位），则进行按编号查找
	       if (listp.isEmpty()&&!(codeOrReference.length()==0)&&codeOrReference.length()<9){
	        	 oProduct = productDAO.getProductInfoByReference(codeOrReference);
	        	 if(oProduct!=null)
	        		 listp.add(oProduct);
	       } 
		
		if(listp!=null){
			String showSrc = null;
			for(Product p1:listp){
			  showSrc+=getProductDialy(p1,productBuilderType,productDAO);
			}
			productHander.obtainMessage(MSG_SUCCESS,showSrc).sendToTarget();//获取图片失败   
		}
		if(listp.isEmpty())
			productHander.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
		
	}
	
    public static void findProductWithRomote(String codeOrReference,Handler productHander,int productBuilderType){
    	List<Product> listp=null;
    	I_DataLogicSales basic =DataLogicPDA.getDLERP();
		try{
		//查询是否为查包
		   if(codeOrReference.length()==14&&codeOrReference.startsWith("1"))
			   listp = basic.getBomProductForIncludeList(codeOrReference.substring(1));
		   else if(codeOrReference.length()==14&&codeOrReference.startsWith("2"))
			   listp = basic.getBomIncludeProductList(codeOrReference.substring(1));
		  else
			  listp = basic.getProductInfoByCode(codeOrReference);

		   Product oProduct;
	        //当按条码找不到商品时，如果输入的可能是编号（长度小于9位），则进行按编号查找
	       if (listp.isEmpty()&&!(codeOrReference.length()==0)&&codeOrReference.length()<9){
	        	 oProduct = basic.getProductInfoByReference(codeOrReference);
	        	 if(oProduct!=null)
	        		 listp.add(oProduct);
	       } 
		
		if(listp!=null&&!listp.isEmpty()){
			String showSrc = "";
			for(Product p1:listp)
			  showSrc+=getProductDialy(p1,productBuilderType,basic);
			productHander.obtainMessage(MSG_SUCCESS,showSrc).sendToTarget();//获取图片失败   
		}else
			productHander.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
	   } catch (HessianRuntimeException e){
		e.printStackTrace();
		productHander.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
	   }catch(HessianConnectionException e) {
            e.printStackTrace();
            productHander.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败
        }catch (Exception e) {
		e.printStackTrace();
		productHander.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
	  }
	
	}
    
   public static String getProductDialy(Product p1,int productBuilderType,ProductDAO productDAO){
	   String retStr=getProductDialy(p1);
	   return retStr;
   }	
   
   
   public static String getProductDialy(Product p1,int productBuilderType, I_DataLogicSales basic){
	        String retStr=getProductDialy(p1);
			if((productBuilderType&BUILDERTYPE_ERP)==BUILDERTYPE_ERP){
				retStr+="\t进价:"+p1.getPriceBuy()+"\n";
				try {
					List<ProductStoreView> productStores=basic.listByFilter(ProductStoreView.class,p1.getID());
				    for(ProductStoreView ps:productStores){
				    	retStr+="\n仓存:"+ps.getM_Location()+"<"+ps.getM_unit()+">"+ps.getState()+"\n";
				    }
				} catch (BasicException e) {
					e.printStackTrace();
				}
			}
			return retStr;
	  

	 
   }
   
   public static String getProductDialy(Product p1){
	   return p1.getCode()+"\n"+p1.getName()+"\n售价:"+p1.getPriceSell()+"\n会员价:"+p1.getM_dCustomerPrice()+"\n批价:"+p1.getM_dWholesalePrice();
   }
   

}
